package com.youth.mapper;

import com.youth.entity.po.Commodity;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

/**
 * @ClassName ICommodityMaper
 * @Description: TODO
 * @Author yjwu
 * @Date 2019/10/30 23:03
 **/
@Mapper
public interface ICommodityMaper {

    @Insert("INSERT INTO t_commodity (code,name,type_three_id,headline,place_area_id,do_hot,keywords,price," +
            "storage_number,sold_number,description,remark,status,create_time,update_time) " +
            "VALUES(#{code},#{name},#{typeThreeId},#{headline},#{placeAreaId},#{doHot},#{keywords},#{price}" +
            ",#{storageNumber},#{soldNumber},#{description},#{remark},#{status},#{createTime},#{updateTime})")
    @Options(useGeneratedKeys = true, keyColumn = "id")
    Integer add(Commodity commodity);

    @Insert(
            "<script>" +
                    "INSERT INTO t_commodity (code,name,type_three_id,headline,place_area_id,do_hot,keywords,price," +
                    "storage_number,sold_number,description,remark,status,create_time,update_time) " +
                    "VALUES " +
                    "<foreach collection='list' item='commodity' separator=',' >" +
                    "(#{commodity.code},#{commodity.name},#{commodity.typeThreeId},#{commodity.headline},#{commodity.placeAreaId},#{commodity.doHot},#{commodity.keywords},#{commodity.price}," +
                    "#{commodity.storageNumber},#{commodity.soldNumber},#{commodity.description},#{commodity.remark},1,NOW(),NOW())" +
                    "</foreach>" +
                    "</script>")
//    @Options(useGeneratedKeys = true, keyColumn = "id")
    Integer addByList(@Param("list") List<Commodity> list);


    @Update("UPDATE t_commodity SET status=#{status},update_time=#{updateTime} WHERE id=#{id}")
    Integer delete(@Param("code") String code, @Param("status") int status, @Param("updateTime") Date updateTime);

    @Update("UPDATE t_commodity SET code=#{code},name=#{name},type_three_id=#{typeThreeId},headline=#{headline},place_area_id=#{placeAreaId}" +
            ",do_hot=#{doHot},keywords=#{keywords},price=#{price},storage_number=#{storageNumber}" +
            ",description=#{description},remark=#{remark},update_time=#{updateTime} WHERE id=#{id}")
    Integer edit(Commodity commodity);

    @Update("UPDATE t_commodity SET storage_number=#{storageNumber},sold_number=#{soldNumber},update_time=#{updateTime} WHERE code=#{code}")
    Integer decreaseStockNumber(@Param("code") String code, @Param("storageNumber") int storageNumber, @Param("soldNumber") int soldNumber, @Param("updateTime") Date updateTime);


    @Select("SELECT * FROM t_commodity WHERE code=#{code}")
    Commodity findByCode(@Param("code") String code);

    @Select("<script>" +
            "SELECT * FROM t_commodity WHERE status=#{status}  " +
            "<if test='name != \"\"'> " + " AND name LIKE  " +
            "</if>" +
            "<if test='name != null'> " + " WHERE status=#{status} " +
            "</if>" +
            "<if test='name != null'> " + " WHERE status=#{status} " +
            "</if>" +
            "<if test='name != null'> " + " WHERE status=#{status} " +
            "</if>" +
            "</script>")
    List<Commodity> findAllWithConditions(@Param("status") int status, @Param("name") String name, @Param("code") String code,
                                          @Param("beginDate") String beginDate, @Param("endDate") String endDate);
}
